import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'

interface Interceptors {
    reqUse(config: AxiosRequestConfig): AxiosRequestConfig
    resUse(res: AxiosResponse): AxiosResponse | Promise<any>
    resErr(err: any): Promise<any>
}

interface IConfig extends AxiosRequestConfig {
    interceptors?: Interceptors
}

class Ajax {
    instance: AxiosInstance
    constructor(config: IConfig) {
        this.instance = axios.create(config)
        this.instance.interceptors.request.use(config.interceptors?.reqUse)
        this.instance.interceptors.response.use(
            config.interceptors?.resUse,
            config.interceptors?.resErr
        )
    }
}

export default Ajax